<?php
/* 用户授信表 */
namespace Home\Model;

use Common\Library\BusinessException;
use Common\Library\Constant;
use Think\Log;
use Think\Model;

class CreditVettingModel extends Model{

    //信审状态
    const STATUS_MANUAL_REVIEWED     = 1;   //信审复审通过
    const STATUS_WAITING_REVIEW      = 2;   //待审核
    const STATUS_MANUAL_REFUSE       = 3;   //信审拒绝
    const STATUS_AUTO_REFUSE         = 4;   //系统拒绝
    const STATUS_AUTO_REVIEWED       = 5;   //系统自动审核通过
    const STATUS_MARKET_REVIEWED     = 6;   //市场初审通过
    const STATUS_MARKET_REVIEWEDER   = 7;   //市场初审强烈通过
    const STATUS_MARKET_REFUSE       = 8;   //市场拒绝
    const STATUS_MARKET_ASSURE       = 9;   //市场业务员担保
    const STATUS_MANUAL_FIRST_NOPASS = 10;  //信审初审不合格
    const STATUS_MANUAL_FIRST_PASS   = 11;  //信审初审通过
    const STATUS_MANUAL_NEXT_NOPASS  = 12;  //信审复审不合格
    const STATUS_MARKET_NEXT_PASS    = 13;  //市场复审通过
    const STATUS_MANUAL_CREDIT_ERR   = 14;  //市场初审-用户运营商认证失败

    public function getCreditVetting($customerId){
        $res = M('CreditVetting')->where(['customer_id'=>$customerId])->Order('id desc')->find();
        return $res;
    }

    public function getCreditInfo($customerId)
    {
        if(!($credit = $this->getCreditVetting($customerId))){
            $creditData['status'] = 1;
        }else{
            $creditData['status'] = $credit['status'];
            //自动拒绝
            if(ConsumeHistoryModel::STATUS_TONDDUN_FAILED == $credit['status']){
                $creditData['reason'] = $credit['reason'];
                $creditData['notice_status'] = $credit['notice_status'];
            }
            //人工拒绝
            if(ConsumeHistoryModel::STATUS_REVIEW_FAILED == $credit['status']){
                $creditData['reason'] = $credit['reason'];
                $creditData['valuation'] = $credit['valuation'];
                $creditData['notice_status'] = $credit['notice_status'];
            }
            //待审核
            if(ConsumeHistoryModel::STATUS_WAIT_REVIEWED == $credit['status']){
                //提升额度
                if($credit['consume_id'] == 0){
                    $creditData['amount'] = $credit['credit_amount'];
                    $creditData['notice_status'] = $credit['notice_status'];
                    $creditData['add_time'] = $credit['add_time'];
                    $creditData['type'] = Constant::PAY_TYPE_ZERO_APPLY;
                }else{
                    //获取消费订单详情
                    $consume = D('ConsumeHistory')->getConsumeHistory($credit['consume_id'], $credit['status']);
                    if(!$consume){
                        BusinessException::throwException(Constant::INTERNAL_ERROR_MYSQL);
                    }
                    $creditData['amount'] = $consume['amount'];
                    $creditData['notice_status'] = $credit['notice_status'];
                    $creditData['add_time'] = $consume['add_time'];
                    $creditData['type'] = $consume['type'];
                    $creditData['merchant_id'] = $consume['merchant_id'];
                    $creditData['merchant_name'] = $consume['merchant_name'];
                    $creditData['consume_amount'] = $consume['consume_amount'];
                    $creditData['reduce_amount'] = $consume['reduce_amount'];
                    $creditData['repayment_time'] = $consume['repayment_time'];
                }
            }
        }
        return $creditData;
    }

    /** 笔数优惠 **/
    public function getDiscountCountByOpr($opr_id)
    {
        $sql = "SELECT count(1) AS cnt FROM credit_vetting v
                LEFT JOIN consume_history h on v.consume_id = h.id
                WHERE v.status in (".CreditVettingModel::STATUS_MARKET_REVIEWED.", ".
                CreditVettingModel::STATUS_MARKET_REVIEWEDER.") AND left(v.add_time, 7)= '".
                date('Y-m', time())."' AND h.`status` in (".ConsumeHistoryModel::STATUS_REVIEWED.", 
                ".ConsumeHistoryModel::STATUS_PAY_OK.", ".ConsumeHistoryModel::STATUS_NEXT_INSTALL.
                ", ".ConsumeHistoryModel::STATUS_WAIT_TWO.") AND h.discount_interest = 2 AND opr_id = ".$opr_id;

        $count = M()->query($sql);
        Log::write("sql=".$sql.'==='.json_encode($count), Log::DEBUG);

        return $count[0]['cnt'];
    }
}